package com.daoeping.dao;

import com.daoeping.data.Bzn_question;
import com.daoeping.data.Bzn_questioncomm;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.json.JSONArray;

import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Bzn_questioncommDAO extends AllDaoSupport {
	public JsonData findAllBzn_questioncommByPage (Page page, TableData tableData, Bzn_question bzn_question){
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append("select   bzn.content , user.username,  bzn.id ,user.id  from Bzn_questioncomm bzn , Usr_user  user where user.id = bzn.usr_user.id and bzn.bzn_question.id = '"+bzn_question.getId()+"'");
			sb2.append("select count(bzn) from Bzn_questioncomm bzn where  bzn.bzn_question.id = '"+bzn_question.getId()+"'");
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(
					page, sb1.toString());
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public boolean delBzn_questioncommByQuestionId(Bzn_question bzn_question){
		return updateDataByHQL("delete from Bzn_questioncomm comm where comm.bzn_question.id ="+bzn_question.getId());
	}
}